const arr = [1, 2, 3, 3, 2, 1, 2, 1]

function count (arr) {
  return arr.reduce((pre, item) => {
    if (!(item in pre)) {
      pre[item] = 1
    } else {
      pre[item] += 1
    }

    return pre
  }, {})
}

console.log(count(arr))
